﻿using System;
using System.Data.SqlClient;
using BikeBouncer.BusinessLayer.Entities;
using BikeBouncer.BusinessLayer.Enums;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader User_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@UserId", id);

			return (SqlDataReader)db.ExecuteReader("upUser_Get");
		}

		public static SqlDataReader User_Get(String email)
		{
			return User_Get(email, EntityStatus.OnlyActive);
		}

		public static SqlDataReader User_Get(String email, EntityStatus entityStatus)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@Email", email);

			SetEntityStatus(db, entityStatus);

			return (SqlDataReader)db.ExecuteReader("upUser_Get");
		}

		#endregion

		#region Insert and update

		public static void User_Upsert(User user)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@Email", user.Email);
				db.AddParameter("@DisplayName", user.DisplayName, true);
				db.AddParameter("@Active", user.Active);

				if (user.Address.Id > 0)
				{
					db.AddParameter("@AddressId", user.Address.Id, true);
				}
				else
				{
					db.AddParameter("@AddressId", null, true);
				}

				if (user.Id > 0)
				{
					db.AddParameter("@UserId", user.Id);

					db.ExecuteScalar("upUser_Update");
				}
				else
				{
					db.AddParameter("@PasswordHash", User.GetPasswordHash(user.Email, user.Password));

					user.Id = Convert.ToInt32(db.ExecuteScalar("upUser_Insert"));

					MailManager.UserRegistrationComplete(user);
				}
			}
		}

		public static void User_UpdatePassword(User user)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@UserId", user.Id);
				db.AddParameter("@PasswordHash", User.GetPasswordHash(user.Email, user.Password));

				db.ExecuteScalar("upUser_UpdatePassword");
			}
		}

		public static void User_UpdateLoginDate(User user)
		{
			if (user.Id < 1)
			{
				return;	
			}

			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@UserId", user.Id);

				db.ExecuteScalar("upUser_UpdateLoginDate");
			}
		}

		public static void User_UpdateLoginDateAsync(Object user)
		{
			if (user != null)
			{
				User_UpdateLoginDate((User) user);
			}
		}

		#endregion

		#region Password recovery

		public static SqlDataReader UserPasswordRecovery_Get(String key)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@RecoveryKey", key);

			return (SqlDataReader)db.ExecuteReader("upUserPasswordRecovery_Get");
		}

		public static void UserPasswordRecovery_Insert(UserPasswordRecovery rec)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@UserId", rec.User.Id, true);
				db.AddParameter("@RecoveryKey", rec.RecoveryKey);

				db.ExecuteScalar("upUserPasswordRecovery_Insert");
			}
		}

		public static void UserPasswordRecovery_Delete(User user)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@UserId", user.Id);

				db.ExecuteScalar("upUserPasswordRecovery_Delete");
			}
		}

		public static void UserPasswordRecovery_Delete(DateTime beforeDate)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@BeforeDate", beforeDate);

				db.ExecuteScalar("upUserPasswordRecovery_Delete");
			}
		}

		#endregion
	}
}